<?php

/**
 * 登陆
 */

namespace app\topics\middle;


class LoginMiddle extends \app\base\middle\BaseMiddle {


    /**
     * 媒体信息
     */
    protected function meta() {
        $this->setMeta('登录');
        $this->setName('会员登录');
        $this->setCrumb([
            [
                'name' => '登录',
                'url' => url()
            ]
        ]);

        return $this->run([
            'pageInfo' => $this->pageInfo
        ]);
    }

    protected function post() {
        $userName = $this->params['username'];
        $password = $this->params['password'];
        $remember = $this->params['remember'];

        if (empty($userName) || empty($password)) {
            return $this->stop('用户名或密码未填写！');
        }

        $password = md5($password);
        $topicsUser = target('topics/TopicsUser')->getWhereInfo([
            'username' => html_clear($userName),
            'password' => $password
        ]);
        if(empty($topicsUser)) {
            return $this->stop('用户名或密码填写错误！');
        }


        target('topics/TopicsUser')->where(['user_id' => $topicsUser['user_id']])->data([
            'login_time' => time(),
            'login_ip' => \dux\lib\Client::getUserIp()
        ])->update();

        $config = \dux\Config::get('dux.use');
        $token = sha1($password . $config['safe_key']);
        $uid = $topicsUser['user_id'];

        $time = $remember ? 2592000 : 86400;
        return $this->run([
            'uid' => $uid,
            'token' => $token,
            'time' => $time
        ]);
    }



}